/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.designPatterns.flyweight;
import java.util.Map;
import java.util.HashMap;
/**
 *
 * @author minga
 */
public class FlyWeightFactory {
    private static FlyWeightFactory flyweightFactory;
    private Map<String,FlyWeight> flyweightPool;
    public FlyWeightFactory()
    {
       flyweightPool = new  HashMap<String,FlyWeight>();
    }
    public static FlyWeightFactory getInstance()
    {
        if(flyweightFactory==null)
        {
           flyweightFactory = new FlyWeightFactory(); 
        }
        return flyweightFactory;
    }
    public FlyWeight getFlyWeight(String key)
    {
        if(flyweightPool.containsKey(key))
        {
            return flyweightPool.get(key);
        }
        else
        {
           FlyWeight  flyweight;
          if("add".equals(key))
          {
              flyweight = new FlyWeightAdder();
          }
          else
          {
              flyweight = new FlyWeightMultiplier();
          }
          flyweightPool.put(key, flyweight);
          return flyweight;
        }
    }
}
